package com.x.erp.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.x.erp.model.Car;
import com.x.erp.model.ClientSelectTemplate;
import com.x.erp.model.SelectTemplate;
import com.x.erp.rpc.ClientCarRpcService;
import com.x.erp.rpc.SelectTemplateRpcService;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 查询模板
 */
@RestController
@RequestMapping("/selectTemplate")
public class SelectTemplateController extends BaseController{

	@Reference
	private SelectTemplateRpcService selectTemplateRpcService;// 大屏智能机器人信息服务

	@Reference
	private ClientCarRpcService clientCarRpcService;// C端车辆信息服务

	@RequestMapping(value = {"/getSelectTemplateById"}, method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel selectSelectTemplateById(SelectTemplate selectTemplate) {
		if(StringUtils.isEmpty (selectTemplate.getTemplateId())){
			return new ResultModel(ResultCode.RESULT_FAIL, "模板Id不能为空！");
		}
		// 先查询出订阅信息
		selectTemplate = selectTemplateRpcService.selectSelectTemplate(selectTemplate);
		if(selectTemplate != null){
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功！", selectTemplate);
		}
		return new ResultModel(ResultCode.RESULT_FAIL, "id错误");
	}

	@RequestMapping(value = {"/getCarListBySelectTemplate"}, method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel selectCarListBySelectTemplate(Car car, String templateId) {
		if(StringUtils.isEmpty (templateId)){
			return new ResultModel(ResultCode.RESULT_FAIL, "模板Id不能为空！");
		}
		if(car.getPageIndex() == null){
			car.setPageIndex(1);
		}
		if(car.getPageSize() == null){
			car.setPageSize(15);
		}
		// 先查询出订阅信息
		SelectTemplate selectTemplate = new SelectTemplate();
		selectTemplate.setTemplateId(templateId);
		selectTemplate = selectTemplateRpcService.selectSelectTemplate(selectTemplate);
		if(selectTemplate == null){
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功！", new ArrayList<>());
		}
		// 讲订阅信息复制到erp的客户端订阅信息类里
		ClientSelectTemplate temp = new ClientSelectTemplate();
		BeanUtils.copyProperties(selectTemplate, temp);
		// 根据C端订阅信息查询车辆列表
		List<Car> res = clientCarRpcService.selectCarListBySelectTemplate(car, temp);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功！", res);
	}
}
